﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using System.Web;
using Wuqi.Webdiyer;
using NLBS.Resources;
using NLBS.Text;

namespace NLBS.Web
{
   public class ViewHelp
    {
       public static bool checkViewPermission(Article objArticle)
       {
           Right power = BlogInfo.Instance().CurrentUser.Group.Power;
           bool loginIn = HttpContext.Current.User.Identity.IsAuthenticated;
           return !(
                     (
                       objArticle.Mode == 1 && power.View < 1 ||
                       objArticle.Mode == 2 && (!loginIn || power.View < 1) ||
                       (objArticle.Mode == 3 || objArticle.Category.Hidden) && (!loginIn || power.View < 2) ||
                       objArticle.Mode == 4 && (!loginIn || power.View < 3)
                     ) && objArticle.AuthorID != BlogInfo.Instance().CurrentUser.UserID
                   );
       }

       public static bool checkCommentViewPermission(Comment objComment)
       {
           Right power = BlogInfo.Instance().CurrentUser.Group.Power;
           bool loginIn = HttpContext.Current.User.Identity.IsAuthenticated;
           int log_mode = objComment.Article.Mode;
           bool hidden = objComment.Hidden;
           bool cat_hidden = objComment.Article.Category.Hidden;

           return (
                     log_mode == 1 && (hidden || cat_hidden) && power.View > 1 ||
         log_mode == 1 && !hidden && !cat_hidden && power.View > 0 ||
         log_mode == 2 && !hidden && !cat_hidden && loginIn && power.View > 0 ||
         log_mode == 3 && loginIn && power.View > 1 ||
         log_mode == 4 && loginIn && power.View > 2
                   );
       }

       public static bool checkCommentEditPermission(Comment item)
       {
           User user = BlogInfo.Instance().CurrentUser;
           int groupEdit = user.Group.Power.Edit;
           return (groupEdit > 1 ||
                  groupEdit == 1 && (user.UserID == item.AuthorID) ||
                  user.UserID == item.AuthorID);
       }

       public static bool checkCommentDeletePermission(Comment item)
       {
           User user = BlogInfo.Instance().CurrentUser;
           int groupDelete = user.Group.Power.Delete;
           return (groupDelete > 1 ||
                   groupDelete == 1 && (user.UserID == item.AuthorID) ||
                   user.UserID == item.AuthorID);
       }

       public static void RenderCommentActionBtn(Comment item, StringBuilder sb)
       {
           UrlFormatsProvider urlFormats = UrlFormatsProvider.Instance();
           if (BlogInfo.Instance().CurrentUser != null)
           {
               if(checkCommentEditPermission(item))
               {
                   RenderActionBtn("edit", sb, urlFormats.CommentEditUrl(item.CommentID, item.LogID),"edit");
               }
               if(checkCommentDeletePermission(item))
               {
                   if (RequestWrapper.GetQueryInt("id") > 0)
                   {
                       RenderActionBtn("delete", sb, urlFormats.CommentDeleteUrl(item.CommentID, item.LogID), "del");
                   }
                   else
                   {
                       RenderActionBtn("delete", sb, urlFormats.CommentDeleteUrl(item.CommentID, 0), "del");
                   }
               }
           }
       }

       public static void RenderActionBtn(string key, StringBuilder sb,string url,string img)
       {
           Dictionary<string, string> lang = Language.Instance().getLanguage();
           sb.AppendFormat("<a onclick=\"{0}\" href=\"javascript:void(0)\" title=\"{1}\">",
                                  url, lang[key]);
           sb.AppendFormat("<img src=\"/styles/default/images/icon_{1}.gif\" alt=\"{0}\" /></a>", lang[key],img);
       }

       public static string RenderComment(Comment item)
       {
           UrlFormatsProvider urlFormats = UrlFormatsProvider.Instance();
           StringBuilder sb = new StringBuilder();
           sb.AppendFormat("<div class=\"commentbox\" id=\"comment{0}\">", item.CommentID);
           sb.AppendFormat("<a name=\"comment{0}\"></a>", item.CommentID);
           sb.Append("<div class=\"commentbox-title\">");
           string doQuote = string.Empty;
           string quote_author = "<img src=\"/styles/default/images/icon_quote_author.gif\" alt=\"引用\" />";
           string icon_quote = "<img src=\"/styles/default/images/icon_quote.gif\" alt=\"引用\" />";
           if (RequestWrapper.GetQueryInt("id") > 0)
           {
               doQuote = "<a href=\"javascript:doQuote('comm_{0}','{1}')\">{2}</a>";

               //sb.Append("<img src=\"/styles/default/images/icon_quote_author.gif\" alt=\"引用\" /></a>");
               if (item.AuthorID == 0)
               {
                   sb.AppendFormat(doQuote, item.CommentID, item.Author, quote_author);
                   sb.AppendFormat("<b>{0}*</b>", item.Author);
               }
               else
               {
                   sb.AppendFormat(doQuote, item.CommentID, item.Author, icon_quote);
                   sb.AppendFormat("<b><a onclick=\"{0}\" href=\"#\">{1}</a></b>", urlFormats.UserDetail(item.AuthorID), item.Author);
               }

           }
           else
           {
               if (item.AuthorID == 0)
               {
                   sb.Append(icon_quote);
                   sb.AppendFormat("<b>{0}*</b>", item.Author);
               }
               else
               {
                   sb.Append(quote_author);
                   sb.AppendFormat("<b><a onclick=\"{0}\" href=\"#\">{1}</a></b>", urlFormats.UserDetail(item.AuthorID), item.Author);
               }
           }

           sb.Append("<div class=\"commentbox-label\">");
           sb.AppendFormat("[{0} ", item.PostTime);
           if (BlogInfo.Instance().CurrentUser.GroupID == 1) sb.Append("| "+item.IP);
           sb.Append("]");
           if (HttpContext.Current.Request.IsAuthenticated) ViewHelp.RenderCommentActionBtn(item, sb);

           sb.Append("</div></div>");
           if (item.Hidden && (item.AuthorID != BlogInfo.Instance().CurrentUser.UserID || item.AuthorID == 0))
           {
               sb.AppendFormat("<div id=\"comm_{0}\" class=\"commentbox-content\">{1}</div>", item.CommentID, "这是一个隐藏评论。");
           }
           else
           {
               sb.AppendFormat("<div id=\"comm_{0}\" class=\"commentbox-content\">{1}</div>", item.CommentID,LBSTextHelper.lbsUBB(item.Content,item.UbbFlags));
           }
           sb.Append("</div>");
           return sb.ToString();
       }

    }
}
